package com.jdd.enterprise.api.vo;

import com.jdd.enterprise.exception.BasicException;
import com.jdd.enterprise.util.BasicRespCode;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 *
 * @author xxxx
 *
 */
@Getter
@Setter
public class ApiResponse<T> implements Serializable {
	private static final long serialVersionUID = 1L;
	/** 返回码: 0000成功  **/
	private String code = BasicRespCode.SUCCESS.code;
	/** 返回信息  **/
	private String msg = null;
	/** 追踪号  **/
	private String trace;
	/** 返回数据  **/
	private T data = null;
	public ApiResponse() {
		super();
	}
	public ApiResponse(String code, String msg, T data) {
		super();
		this.code = code;
		this.msg = msg;
		this.data = data;
	}
    public static <T> ApiResponse<T> fail(String msg){
    	return new ApiResponse<T>(BasicRespCode.FAIL.code, msg, null);
    }
    public static <T> ApiResponse<T> fail(String code, String msg){
    	return new ApiResponse<T>(code, msg, null);
    }
    public static <T> ApiResponse<T> fail(BasicException e){
    	return new ApiResponse<T>(e.getErrorCode(), e.getErrorMsg(), null);
    }
    public static <T> ApiResponse<T> success(){
    	return new ApiResponse<T>(BasicRespCode.SUCCESS.code, null, null);
    }
    public static <T> ApiResponse<T> success(T data){
    	return new ApiResponse<T>(BasicRespCode.SUCCESS.code, null, data);
    }
	public static boolean isSuccess(ApiResponse<?> resp) {
		return resp != null && BasicRespCode.SUCCESS.code.equals(resp.getCode());
	}
	public static boolean isSuccessAndNotNull(ApiResponse<?> resp) {
		return resp != null && resp.getData() != null && BasicRespCode.SUCCESS.code.equals(resp.getCode());
	}
	public static boolean isFail(ApiResponse<?> resp) {
		return !isSuccess(resp);
	}
}
